package cn.handong.tool.多线程;

import java.util.concurrent.*;

public class ThreadTest {

    public static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main....start.....");
        CompletableFuture.runAsync(()->{
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        },executorService);
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
            return i;
        }, executorService);
        Integer integer = future1.get();

        System.out.println("main....stop....." + integer);
        System.out.println("##############################################");

        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 0;
            System.out.println("运行结果：" + i);
            return i;
        }, executorService).whenComplete((res,exception) ->{
            // 虽然能得到异常信息，但是没法修改返回的数据
            System.out.println("异步任务成功完成了...结果是：" +res + "异常是：" + exception);
        }).exceptionally(throwable -> {
            // 可以感知到异常，同时返回默认值
            return 10;
        });
    }

}
